﻿using System.Web;
using Summer.Web.Context;
using Summer.Core.Ioc;
using Summer.Core.Config;

#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.Web.Support
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009-11-09 23:43:08
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.Web.Support
{
    /// The class is only used in web application
    /// In windows, the method is from  Summer.Windows.Support.ApplicationStateSupport
    public static class ApplicationStateSupport
    {
        /// <summary>
        /// Load base container in web
        /// </summary>
        /// <param name="application"></param>
        /// <param name="m_contextxml"></param>
        /// <param name="configuration"></param>
        public static void LoadContainer(this HttpApplicationState application,string m_contextxml,IocConfiguration configuration)
        {
            //load config file
            SummerConfig config = configuration.GetConfigFile(m_contextxml);
            //Create new contextContainer
            AbstractContextContainer<AbstractContextContainer<IContextObject>> baseContainer = (AbstractContextContainer<AbstractContextContainer<IContextObject>>)configuration.GetRootContainer(config);
            //Load the baseContainer
            baseContainer.Load(application);
            AbstractContextContainer<IContextObject>[] containers = configuration.GetContainers(config);
            foreach (AbstractContextContainer<IContextObject> obj in containers)
            {
                configuration.SetContext(obj, config,baseContainer);
            }
            //Add the base container into boarding container
            //In web,the parent container is ApplicationState
            application.Add(IocConst.BASE_CONTAINER, baseContainer);
        }
        /// <summary>
        /// Unload base container in web
        /// </summary>
        /// <param name="application"></param>
        public static void UnloadContainer(this HttpApplicationState application)
        {
            WebContextContainer baseContainer = application.GetContainer();
            if (baseContainer != null)
            {
                baseContainer.UnLoad();
            }
        }
        /// <summary>
        /// Get the root container from application
        /// </summary>
        /// <param name="application"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static WebContextContainer GetContainer(this HttpApplicationState application, string key)
        {
            return (WebContextContainer)application[key];
        }
        /// <summary>
        /// Get the root container from application
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        public static WebContextContainer GetContainer(this HttpApplicationState application)
        {
            return (WebContextContainer)application[IocConst.BASE_CONTAINER];
        }
        /// <summary>
        /// Get the default context which contains contextObject
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        public static Summer.Core.Ioc.Context GetDefaultContextContainer(this HttpApplicationState application)
        {
            return (Summer.Core.Ioc.Context)application.GetContainer()[IocConst.DEFAULT_CONTEXT];
        }
        /// <summary>
        /// Get the config context which contains contextObject
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        public static Summer.Core.Ioc.Context GetConfigContextContainer(this HttpApplicationState application)
        {
            return (Summer.Core.Ioc.Context)application.GetContainer()[IocConst.CONFIG_COTEXT];
        }
        /// <summary>
        /// Get Context by name
        /// </summary>
        /// <param name="application"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Summer.Core.Ioc.Context GetContextContainerByName(this HttpApplicationState application,string name)
        {
            return (Summer.Core.Ioc.Context)application.GetContainer()[name];
        }

        public static IContextObject GetContextObject(this Summer.Core.Ioc.Context context,string key)
        {
            return context[key];
        }
    }
}
